package com.gadgets.busi.bm03;

import com.gadgets.framework.common.util.FileOperUtil;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能说明：
 * 软件版权：恒生电子股份有限公司
 * 日志打印代码扫描
 * @author xiujunqing
 * @version V202101.00.000
 * @data 2022-04-12
 * 修改记录：  修改日期          修改人员    修改说明
 * 2022-04-12  xiujunqing   创建文件
 */
public class LogPrintNumber {

    private static final int LIMIT = 25;

    private static final HashMap<String,Integer[]> moduel = new HashMap<>();

    private static final Integer[] totalNum = new Integer[]{0,0};

    static {
        moduel.put("bbsp-cp-common", new Integer[]{0,0});
        moduel.put("bbsp-cp03", new Integer[]{0,0});
        moduel.put("bbsp-cp07", new Integer[]{0,0});
        moduel.put("bbsp-cp08", new Integer[]{0,0});
        moduel.put("bbsp-cp09", new Integer[]{0,0});
        moduel.put("bbsp-cp11", new Integer[]{0,0});
        moduel.put("bbsp-cp12", new Integer[]{0,0});
        moduel.put("bbsp-cp14", new Integer[]{0,0});
        moduel.put("bbsp-cp-atte", new Integer[]{0,0});
        moduel.put("bbsp-dt05", new Integer[]{0,0});
        moduel.put("bbsp-dt07", new Integer[]{0,0});
        moduel.put("bbsp-ep20", new Integer[]{0,0});
        moduel.put("bbsp-ep21", new Integer[]{0,0});
        moduel.put("bbsp-ep23", new Integer[]{0,0});
        moduel.put("bbsp-ep24", new Integer[]{0,0});
        moduel.put("bbsp-ep25", new Integer[]{0,0});
        moduel.put("bbsp-ep28", new Integer[]{0,0});
        moduel.put("bbsp-ep36", new Integer[]{0,0});
    }

    public static void main(String[] args) {
        File root = new File("F:\\Code\\Hundsun\\cpes4.0");
        LogPrintNumber.FileLoad(root);
        FileOperUtil.writeTxtTotFile("D:\\log.txt","\n\n\n\n日志输出量\n");
        for (Map.Entry<String, Integer[]> entry : moduel.entrySet()) {
            FileOperUtil.writeTxtTotFile("D:\\log.txt", entry.getKey()+"总代码行数："+entry.getValue()[1]+"，\t\t日志输出代码行数："+entry.getValue()[0]+"\n");
        }
    }

    public static void FileLoad(File rootPath) {
        if (rootPath.isDirectory()) {
            if(rootPath.getPath().contains(".idea") || rootPath.getPath().contains(".svn") || rootPath.getPath().contains("target")){
                return;
            }
            File[] sonFile = rootPath.listFiles();
            for (File son : sonFile) {
                FileLoad(son);
            }
        } else {
            System.out.println("---------------------beg-----------------------");
            FileOperUtil.writeTxtTotFile("D:\\log.txt","---------------------beg-----------------------\n");
            System.out.println("文件:" + rootPath);
            FileOperUtil.writeTxtTotFile("D:\\log.txt","文件:" + rootPath + "\n");
            if(rootPath.getName().endsWith(".java")) {
                BufferedReader bufferedReader = null;
                FileInputStream inputStream = null;
                InputStreamReader inputStreamReader = null;
                try {
                    inputStream = new FileInputStream(rootPath);
                    inputStreamReader = new InputStreamReader(inputStream);
                    bufferedReader = new BufferedReader(inputStreamReader);
                    String text = null;
                    // 文件日志总行数
                    int i = 0;
                    while (null != (text = bufferedReader.readLine())) {
                        if(
                                text.contains("logger.debug(")
                                ||text.contains("logger.info(")
                                ||text.contains("logger.warn(")
                                ||text.contains("logger.error(")
                                ||text.contains("logger.trace(")
                                ||text.contains("Ct10ServiceInvoker.trace(")
                        ){
                            i++;
                            for (Map.Entry<String, Integer[]> entry : moduel.entrySet()) {
                                if(rootPath.getPath().contains(entry.getKey())){
                                    // 如果属于当前模块，则更新
                                    entry.getValue()[0] = entry.getValue()[0]+1;
                                    break;
                                }
                            }
                            FileOperUtil.writeTxtTotFile("D:\\log.txt","类"+rootPath.getPath()+"存在日志输出，累计数量为："+i+"\n");
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (null != inputStream) {
                            inputStream.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (null != inputStreamReader) {
                        try {
                            inputStreamReader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (null != bufferedReader) {
                        try {
                            bufferedReader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            FileOperUtil.writeTxtTotFile("D:\\log.txt","---------------------end-----------------------\n");
            System.out.println("-----------------end------------------------");
        }
    }

}
